package de.karlNet.executor.scanner.controller;

import java.io.File;
import java.io.FilenameFilter;

import de.karlNet.executor.scanner.io.AppWriter;

public class Controller {
	private String baseDir = "/Applications";
	private AppWriter appWriter = new AppWriter();
	public void scanForApplicatons() {
		this.scanForApplicatons(this.baseDir);
		this.appWriter.close();
	}
	
	public void scanForApplicatons(String baseDir) {
		File dir = new File(baseDir);
		dir.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.endsWith(".app")) {
					int startOfApp = name.indexOf(".app");
					String newName = name.substring(0, startOfApp);
					newName.replace(" ", "\\ ");
					appWriter.writeEntry(newName, dir.getAbsolutePath() + "/" + name);
					return true;
				} else {
					if (new File(dir.getAbsolutePath() + "/" + name)
							.isDirectory()) {
						scanForApplicatons(dir.getAbsolutePath() + "/" + name);
					}
				}
				return false;
			}
		});

	}
}
